package com.zz.ecommerce.declareManage.importB2CDeclareDirect.personalDeclMgnQdc;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.qdc.db.TOrderHeadQdc;
import com.zz.ecommerce.common.constants.DictConstants;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.TDeclformHeadQdcDao;
import com.zz.ecommerce.declareManage.common.service.QdcOrderService;
import com.zz.ecommerce.declareManage.exportB2CDeclare.declareManage.CbecElistDao;
import com.zz.ecommerce.pojo.CbecElist;
import com.zz.ecommerce.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 个人申报清单Controller
 * <p/>
 * Created by zhangguowei on 2016/3/28.
 */
@Controller
@RequestMapping("/importB2CDirect/personalDeclQdc")
@Scope("prototype")
public class PersonalDeclDirectQdcController {

    @Autowired
    private TDeclformHeadQdcDao tDeclformHeadQdcDao;

    @Autowired
    private PersonalDeclDirectQdcService personalDeclDirectQdcService;

    @Autowired
    private QdcOrderService orderService;

    @Autowired
    private CbecElistDao cbecElistDao;

    /**
     * 申报清单管理
     *
     * @return
     */
    @RequestMapping(value = "declarationForm", method = {RequestMethod.GET})
    public String declarationForm() {
        return "declareManage/importB2CDeclareDirect/personalDeclQdc/declaration_form";
    }

    /**
     * 查询申报清单管理列表
     *
     * @param cbecElist
     * @param request
     * @return
     */
    @RequestMapping("/queryDeclareList")
    @ResponseBody
    public GridResult queryDeclareList(CbecElist cbecElist, GridRequest request, HttpSession session) {
        cbecElist.setOrgId(SessionUtil.getOrganizationId(session));
        return new GridResult(cbecElistDao.page(TableConstants.CBEC_ELIST, cbecElist, request.getStart(), request.getEnd(), request.getSortOrder()), request, cbecElistDao.count(TableConstants.CBEC_ELIST, cbecElist));
    }

    /**
     * 记录清单任务表
     *
     * @param id
     * @return
     */
    @RequestMapping("/declare")
    @ResponseBody
    public boolean declare(String id) {
        String[] idArr = id.split(",");
        boolean result = true;
        for (int i = 0; i < idArr.length; i++) {
            if (!personalDeclDirectQdcService.declare(idArr[i])) {
                result = false;
            }
        }
        return result;
    }

    @RequestMapping(value = "generateDeclaration", method = {RequestMethod.GET})
    public String generateDeclaration() {
        return "declareManage/importB2CDeclareDirect/personalDeclQdc/generateDeclare/index";
    }

    @RequestMapping("/generateDeclareList")
    @ResponseBody
    public GridResult generateDeclareList(TOrderHeadQdc orderHead, GridRequest request, HttpSession session) {
//        orderHead.setOperId(SessionUtil.getOperator(session));
        orderHead.setOrgId(SessionUtil.getOrganizationId(session));
        orderHead.setIeFlag(DictConstants.IMPORT);
        orderHead.setBusinessType(DictConstants.B2C);
        orderHead.setTradeMode(DictConstants.TRADE_MODE_NORMAL);
        orderHead.setIfCreate("0");
        return orderService.queryOrderList(request, orderHead);
    }


}
